package com.lhn.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @author lhn
 * @date 2024-08-21 16:05
 */
@Component
@Aspect
public class MyAdvice {

    @Pointcut("execution(* com.lhn.dao.BookDao.update(..))")
    private void pc() {
    }

    @Pointcut("execution(* com.lhn.dao.BookDao.select(..))")
    private void pc_1() {
    }

    @Before("pc()")
    public void before() {
        System.out.println("before advice ...");
    }


    @After("pc()")
    public void after() {
        System.out.println("after advice ...");
    }

    /**
    * @author
    * @date 2024/8/21 16:30
     * @param proceedingJoinPoint 连接点
    */
    @Around("pc()") //环绕通知
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("around before advice ...");
        Object o = proceedingJoinPoint.proceed();//执行目标方法
        System.out.println("around after advice ...");
        return o;
    }

    @Around("pc_1()") //环绕通知
    public Object around_Select(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("around before advice ...");
        Object o = proceedingJoinPoint.proceed();//执行目标方法，获取到原来方法的返回值
        System.out.println("around after advice ...");
        return o;
    }

    @AfterReturning("pc()")
    public void afterReturning() {
        System.out.println("afterReturning advice ...");
    }

    @AfterReturning("pc_1()")
    public void afterReturning_Select() {
        System.out.println("afterReturning advice ...");
    }

    //在BookDaoImpl的update方法上抛出异常时，执行afterThrowing方法
    @AfterThrowing("pc_1()")
    public void afterThrowing_Select() {
        System.out.println("afterThrowing advice ...");
    }
}
